﻿package org.openChart.display 
{ 
	import flash.sampler.NewObjectSample;
	import org.openChart.display.DisplayColorLabelGroup;
	import org.openChart.meta.BarChart;
	import org.openChart.meta.BaseChart;
	import org.openChart.meta.PieChart;
	import org.openChart.meta.XAxis;
	import org.openChart.meta.YAxis;
	import org.openChart.util.Rectangle;
	
	/**
	 * The region to show the chart, it mainly contains three parts: title,
	 * color labels and the chart. We may want to show several types of chart
	 * on this screen, such as, a bar chart showing data of this year and another
	 * line chart showing the increacement. Each chart has its own coordinate,
	 * and we using this coordinate to caculate how to painting the elements in this chart.
	 * 
	 * The screen has a sequence of charts to display and only one top chart, that is, only
	 * for the top chart we paiting the coordinate. Also, we can change the top chart when 
	 * some event occurs.
	 * 
	 * ---------------------------------------------
	 * |                Title                       |
	 * ---------------------------------------------
	 * |  label,  label .....                       |
	 * ---------------------------------------------
	 * |                                            |
	 * |            Chart coordinates               |
	 * |                                            |
	 * ---------------------------------------------
	 * @author chongyuan
	 */
	public class DisplayScreen// implements DisplayInterface
	{
		// The back ground color of screen.
		private var bgcolor_:Number = 0xFFFFFF;
		// The minimum width and height.
		private const minWidth:Number = 150;
		private const minHeight:Number = 150;
		
		// The bottom of chart coordinate.
		private var bottom_:Number = 0;
		// The top of chart coordinate.
        private var top_:Number = 0;
		// The left of chart coordinate.
		private var left_:Number = 0;
		// The right of chart coorinate.
		private var right_:Number = 0;
		
		// The group of color labels.
		private var labels_:DisplayColorLabelGroup;
		// The title of the chart.
		private var title_:DisplayScreenTitle;
		
		// The charts derived from the main chart.
		private var charts_:Array;
		// The main chart in this form. One form contains only one main chart.
		private var topChart_:DisplayChart;
		
		/**
		 * 
		 * @param	width The width of window dialog.
		 * @param	height The height of window dialog.
		 */
		public function DisplayScreen(top:Number, bottom:Number, left:Number, right:Number, chart_:BaseChart) 
		{
			this.topChart_ = createChart(chart_);
			this.bgcolor_ = topChart_.color;
			setSize(top, bottom, left, right);
		}
		
		/**
		 * The initial funciton of displayScreen.
		 */
		public function initialize():void 
		{	
			title_ = new DisplayScreenTitle(topChart_.chart.title, this);
			title_.align = "center-top";
			title.initialize();
			
			labels_ = new DisplayColorLabelGroup(null, this);
			
			topChart_.initialize();
			labels.initialize();
			/*
			for (var i:uint = 0; i < this.charts_.length; i++)
			{
				var chart:DisplayChart = charts_[i] as DisplayChart;
				chart.initialize();
			}
			*/
		}
		
		/**
		 * 
		 * @param	rectangle
		 */
		public function resize():void
		{
			var rectangle_:Rectangle = new Rectangle(top, bottom, left, right);
			title_.resize(rectangle_);
			labels_.resize(rectangle_);
			topChart_.resize(rectangle_);
		}
		
		/**
		 * Set the size of the form.
		 * @param	top The top of form.
		 * @param	bottom The bottom of form.
		 * @param	left The left of from.
		 * @param	right The right of from.
		 */
		public function setSize(top:Number, bottom:Number, left:Number, right:Number):void
		{
			if (top < 0 || bottom < 0 || left < 0 || right < 0 )
			{
				throw new Error("The value is less than zero!");
			}
			if (top > bottom || left > right)
			{
				throw new Error("The area is not valid!");
			}
			var width:Number = right - left;
			var height:Number = bottom - top;
			if (width < minWidth || height < minHeight)
			{
				throw new Error("The area is too small!");
			}
			top_ = top;
			bottom_ = bottom;
			right_ = right;
			left_ = left;	
		}
		
		public function setRectangle(rectangle:Rectangle):void
		{
			setSize(rectangle.top, rectangle.bottom, rectangle.left, rectangle.right);
		}
		
		/**
		 * Creating a display chart model.
		 * 
		 * @param	chart The meta model of display chart.
		 */
		public function createChart( chart:BaseChart ):DisplayChart
		{
			var displayChart:DisplayChart;
			if (chart.chartType.indexOf("bar") >= 0 || chart.chartType.indexOf("line") >= 0)
			{
				var barChart:BarChart = chart as BarChart;
				displayChart = new DisplayBarChart( barChart, this);
			}
			else if(chart.chartType.indexOf("pie") >= 0)
			{
				var pieChart:PieChart = chart as PieChart;
				displayChart = new DisplayPieChart( pieChart, this);
			}
			return displayChart;
		}
		
		public function addChart(chart:DisplayChart):void
		{
			this.charts_.push(chart);
		}
		
		public function get width():Number
		{
			return right_ - left_;
		}
		
		public function get height():Number
		{
			return bottom_ - top_;
		}
		
		public function get top():Number
		{
			return this.top_;
		}
		
		public function set top( top_:Number):void
		{
			this.top_ = top_;
		}
		
		public function get bottom():Number
		{
			return this.bottom_;
		}
		
		public function set bottom(bottom_:Number):void
		{
			this.bottom_ = bottom_;
		}
		
		public function get left():Number
		{
			return this.left_;
		}
		
		public function set left(left_:Number):void
		{
			this.left_ = left_;
		}
		
		public function get right():Number
		{
			return this.right_;
		}
		
		public function set right(right_:Number):void
		{
			this.right_ = right_;
		}
		
		public function getBgcolor():Number
		{
			return this.bgcolor;
		}
		
		public function setBgcolor( bgcolor:Number):void
		{
			this.bgcolor = bgcolor;
		}
		
		
		public function get labels ():DisplayColorLabelGroup
		{
			return this.labels_;
		}
		
		
		public function get title():DisplayScreenTitle
		{
			return this.title_;
		}
		
		public function set title( title_:DisplayScreenTitle ):void
		{
			this.title_ = title_;
		}
		public function get bgcolor():uint
		{
			return bgcolor_;
		}
		
		public function get charts():Array
		{
			return this.charts_;
		}
		
		public function get topChart():DisplayChart
		{
			return topChart_;
		}
		public function set bgcolor( bgcolor_:uint ):void
		{
			this.bgcolor_ = bgcolor_;
		}
		
		public function toString():String
		{
			var str:String = "Object:DisplayScreen ";
			str += "[top = " + top + "] ";
			str += "[left = " + left +"]";
			str += "[right = " + right + "]";
			str += "[bottom= " + bottom + "]";
			str += "[width = " + width + "]";
			str += "[height = " + height + "]";
			str += "[labels = " + labels + "]";
			str += "[title = " + title + "]";
			str += "[topChart = " + topChart + "]";
			return str;
		}
	}
}